from io import BytesIO
import os
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from serve.nlp import ocr_nlp
app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return jsonify({'message': 'No file part in the request'}), 400
    file = request.files['file']
    # 如果用户没有选择文件，浏览器也会提交一个空的表单
    if file.filename == '':
        return jsonify({'message': 'No selected file'}), 400

    if file:
        filename = secure_filename(file.filename)
        filepath = os.path.join('tmp/upload', filename)
        file.save(os.path.join('tmp/upload', filename))
        nlp_data = ocr_nlp('tmp/upload', filename)
        return jsonify(nlp_data), 201



if __name__ == '__main__':
    #app.run(debug=True)
    app.run(debug=True,host='0.0.0.0', port=9887)